Macromedia Flex Article

Learning Flex Basics (Part 2): Creating a Simple Calculator with Macromedia Flex


Table of Contents

  1. Introduction
  2. Building the Calculator
  3. Create the Application Object
  4. Add a Panel for the Calculator
  5. Add the Calculator Display
  6. Add a Grid to Lay Out the Buttons
  7. Add the Calculator Buttons
  8. Create the CalculatorController Class
  9. Add Properties to the Controller Class
  10. Add Controller Methods
  11. Instantiate the CalculatorController Class
  12. Complete Code Listing for calculator.mxml
  13. Complete Code Listing for CalculatorController.as
  14. Where to Go From Here

Instantiate the CalculatorController Class

The final step of the tutorial is to instantiate the CalculatorController class you just created in the calculator.mxml file. You can do this through a tag, but since the tag will not be part of the Flex class library, you will have to add a new namespace for it. You do this by adding an additional xmlns property to the Application tag, giving it a value of "*". The "*" value simply indicates: "accept any class found in the application directory as an element."

Also remember that the CalculatorController class needs a reference to the calculator Application object so that it can access the CalcDisplay object. You pass this reference by setting the calcView property of the CalculatorController object to {this}; in an MXML file, this is a reference to the overall Application object. The curly braces create data binding, so that the Flex compiler evaluates the contents as an expression rather that treating it as a literal string.

Finally, CalculatorController object needs an instance name so that other elements in the calculator can refer to it; the instance name is the id attribute of the tag that instantiates the class.

  1. Return to calculator.mxml and add an additional xmlns property to the Application tag; give the value of the namespace the value of "*" and do not assign a prefix to it:

    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
    
  2. At the beginning of the Application tag block, add a CalculatorController tag with the attributes id="calcController" and calcView="{this}":

    <CalculatorController id="calcController" calcView="{this}"/>
    
  3. Save the file and test it in a web browser (for information on how to browse MXML files, see the first page in this tutorial).